package com.jsle.financial.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.jsle.financial.dao.CompanyDaoImpl;
import com.jsle.financial.domain.Company;
import com.jsle.financial.domain.Company_Resources;
import com.jsle.financial.manager.CompanyManager;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Page;
import com.jsle.financial.vo.Result;
/**
 * CompanyService业务逻辑处理类
 * @author wisdying
 * @date 2012-10-27
 */
@Service
public class CompanyServiceImpl implements CompanyService{

	private Log log = LogFactory.getLog(CompanyServiceImpl.class);
	
	@Resource
	private CompanyManager companyManager;
	@Resource
	private CompanyDaoImpl companyDao;
	
	/**
	 * 新增一公司
	 * @param user
	 * @return
	 */
	public Result addCompany(Company company){
		Result result = new Result();
		try {
			result = companyManager.addCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_INSERT_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_INSERT_SERVICE_ERROR_MESSAGE, "User"));
			log.error("new company error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 删除一公司
	 * @param user
	 * @return
	 */
	public Result delCompany(Company company){
		Result result = new Result();
		try {
			result = companyManager.delCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_MESSAGE, "User"));
			log.error("del company error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 更新一公司信息
	 * @param user
	 * @return
	 */
	public Result updateCompany(Company company){
		Result result = new Result();
		try {
			result = companyManager.updateCompany(company);

		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_UPDATE_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_UPDATE_SERVICE_ERROR_MESSAGE, "User"));
			log.error("update company error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司代码获取一公司信息
	 * @param loginName
	 * @return
	 */
	public Result queryUserByCompanyCode(Company company){
		Result result = new Result();
		try {
			result = companyDao.queryCompanyByCompanyCode(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "User"));
			log.error("query company error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 批量删除公司信息
	 * @param company
	 * @return
	 */
	public Result delCompanys(Company company) {
		return null;
	}
	/**
	 * 根据公司id获取一公司信息
	 * @param loginName
	 * @return
	 */
	public Result queryCompanyByCompanyID(Company company) {
		return companyDao.queryCompanyByCompanyID(company);
	}
	
	/**
	 * 公司配置资源
	 * @param cr
	 * @return
	 */
	public Result addCompanyAndResources(Company_Resources cr){
		Result result = new Result();
		try {
			result = companyDao.addCompanyAndResources(cr);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "User"));
			log.error("add Company_Resources error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 分页查询
	 * @param page
	 * @return
	 */
	public Result queryCompanysByPage(Page page,String companyCode){
		Result result = new Result();
		try {
			result = companyDao.queryCompanysByPage(page,companyCode);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "Query Users by page"));
			log.error("query companys by page error## "+ e.getMessage());
		}
		return result;
	}

	/**
	 * 根据公司id获取公司资源集合
	 */
	public Result getCompanyRes(Company company) {
		return companyDao.getCompanyByCid(company);
	}
	
	/**
	 * 根据公司code删除人员信息
	 * @param company
	 * @return
	 */
	public Result delUsersByCompany(Company company){
		Result result = new Result();
		try {
			result = companyManager.delUsersByCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_MESSAGE, "delUsersByCompany"));
			log.error("delUsersByCompany error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司code删除用户组信息
	 * @param company
	 * @return
	 */
	public Result delUserGroupsByCompany(Company company){
		Result result = new Result();
		try {
			result = companyManager.delUserGroupsByCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_MESSAGE, "delUserGroupsByCompany"));
			log.error("delUserGroupsByCompany error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司code删除公司关联资源信息
	 * @param company
	 * @return
	 */
	public Result delCompanyResourcesByCompany(Company company){
		Result result = new Result();
		try {
			result = companyManager.delCompanyResourcesByCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_MESSAGE, "delCompanyResourcesByCompany"));
			log.error("delCompanyResourcesByCompany error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司code删除人员-用户组关联信息
	 * @param company
	 * @return
	 */
	public Result delUserUserGroupsByCompany(Company company){
		Result result = new Result();
		try {
			result = companyManager.delUserUserGroupsByCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_MESSAGE, "delUserUserGroupsByCompany"));
			log.error("delUserUserGroupsByCompany error## "+ e.getMessage());
		}
		return result;
	}
	public Result queryCompanys() {
		Result result = new Result();
		try {
			result = companyDao.queryCompanys();
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "queryCompanys"));
			log.error("queryCompanys error## "+ e.getMessage());
		}
		return result;
	}
}
